(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.countryCalendar.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'countryCalendarService' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, countryCalendarService) {
		var ctrl = this, countryCalendarInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.countryCalendar = {};
		
		$scope.$watch('viewCalendarYear', function(newValue, oldValue) {
			countryCalendarInfo.calendarDay.year = newValue;
			authService.view(countryCalendarInfo).then(function(response) {				
				angular.merge(ctrl.countryCalendar, response.data);
				$scope.calendarYear = ctrl.countryCalendar.calendarDay.year;
			});
		});
		
		ctrl.init = function() {	
			countryCalendarService.initOptions($scope, ctrl).then(function() {
				return authService.view(countryCalendarInfo).then(function(response) {
					ctrl.countryCalendar = response.data || countryCalendarInfo;
					$scope.calendarYear = ctrl.countryCalendar.calendarDay.year;
					$scope.viewCalendarYear = ctrl.countryCalendar.calendarDay.year;
					ctrl.countryCalendar.calendarDay.yearList = ctrl.countryCalendar.calendarDay.yearList ||[];
					if ( ctrl.countryCalendar.calendarDay.yearList.length == 0) {	
						ctrl.countryCalendar.calendarDay.yearList.push({year: ctrl.countryCalendar.calendarDay.year});
					}
					$scope.yearList = ctrl.countryCalendar.calendarDay.yearList;
				}, angular.noop);
			});
		};
	}
})(angular);